<?php

namespace app\forms;

use app\components\helpers\MappingHelper;
use app\components\validators\PositiveInteger;
use app\models\Role;

/**
 * 角色 父类 表单
 */
class RoleForm extends Model
{
    public $id;
    public $name;
    public $remark;
    public $status;
    public $setDefault = false;
    public $page;
    public $pageSize;

    /**
     * @var array 要赋予的权限项
     */
    public $item;

    /**
     * @var array 赋予的权限项对应的授权数据类型
     */
    public $auth;

    /**
     * @var array 赋予的权限项对应的自定义授权数据
     */
    public $data;

    /**
     * @var integer 权限项ID
     */
    public $itemId;

    /**
     * @var array 权限赋值记录 position_item 表的一条记录
     */
    public $itemData;

    public function rules()
    {
        return [
            /*通用场景*/
            ['id', PositiveInteger::class],

            ['name', 'trim'],
            ['name', 'string', 'max' => 32],

            [['remark', 'name'], 'trim'],
            ['name', 'string', 'max' => 64],

            ['item', 'each', 'rule' => ['integer']],

            ['auth', 'each', 'rule' => ['in', 'range' => array_column(MappingHelper::dataAuthList(), 'id')]],

            ['data', 'each', 'rule' => ['in', 'range' => array_column(MappingHelper::fieldAuthList(), 'id')]],

            ['setDefault', 'boolean'],

            [
                'id',
                'required',
                'on' => ['update', 'delete', 'status', 'view', 'get-info', 'get-item-data', 'get-default-item']
            ],
            [
                'id',
                'exist',
                'targetClass' => Role::class,
                'targetAttribute' => 'id',
                'on' => ['update', 'delete', 'status', 'view', 'get-info', 'get-default-item']
            ],

            /*角色列表*/
            ['name', 'string', 'max' => 16, 'on' => ['role_list']],

            ['status', 'in', 'range' => array_column(MappingHelper::statusList(), 'id')],

            [['page', 'pageSize'], PositiveInteger::class],

            ['page', 'default', 'value' => 1],
            ['pageSize', 'default', 'value' => 10],
            /*添加角色*/
            ['name', 'required', 'message' => t('api', '{attribute} can not be empty'), 'on' => ['create']],
            [
                'name',
                'unique',
                'targetClass' => Role::class,
                'message' => t('api', '{attribute} already exist'),
                'on' => ['create']
            ],
            /*更新角色*/
            ['name', 'checkUnique', 'on' => ['update']],
            ['status', 'required', 'message' => t('api', '{attribute} can not be empty'), 'on' => ['status']],
            /*授权项下部门授权的数据*/
            ['itemId', PositiveInteger::class, 'on' => ['get-item-data']],
            ['itemId', 'required', 'on' => ['get-item-data']],


        ];
    }

    public function scenarios()
    {
        return [
            'index' => ['name', 'status', 'page', 'pageSize'],
            'create' => ['name', 'status', 'remark', 'setDefault', 'item', 'auth', 'data'],
            'update' => ['id', 'name', 'status', 'remark', 'setDefault', 'item', 'auth', 'data'],
            'delete' => ['id'],
            'status' => ['id', 'status'],
            'get-info' => ['id'],
            'get-default-item' => ['id'],
            'get-item-data' => ['id', 'itemId'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => t('api', 'Name'),
            'status' => t('api', 'Status'),
        ];
    }

    public function checkUnique($attribute, $params)
    {
        $exist = Role::find()->select(['id'])->where([
            'and',
            ['name' => $this->$attribute],
            ['!=', 'id', $this->id]
        ])->one();
        if ($exist) {
            $this->addError($attribute, '{attribute} already exist');
        }
    }
}